<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class diy_index_module extends CI_Module
{
    public $data = array();

    public function __construct(){
        parent::__construct();
        $this->load->model('diy.diy_model');
        $this->type = $this->input->get_post('type');
        $this->pluginid = $this->input->get_post('pluginid');
        $this->data = array(
            'form_url' => site_url('diy/index/save?pluginid='.$this->pluginid.'&type='.$this->type),
        );
    }

    public function edit()
    {
        $this->check_admin();
        $this->data['data'] = $this->diy_model->get_data_diy($this->pluginid);
        $this->data['data']['svalue'] = unserialize($this->data['data']['svalue']);
        $this->load->view('admin/edit_'.$this->type, $this->data);
    }


    public function save()
    {
        $this->check_admin();
        $this->diy_model->type     = $this->type;
        $this->diy_model->pluginid = $this->pluginid;

        $id = $this->diy_model->insert(array(
            'sid' => $GLOBALS['sid'],
            'type' => $this->type,
            'pluginid' => $this->pluginid,
            'svalue' => $this->format($_POST),
            'upts' => date('Y-m-d H:i:s'),
        ));
        if($id){
            $this->success('保存成功！', '', true);
        }
    }


    public function format($post)
    {
        return serialize($post);
    }

    public function select_prolist()
    {
        $this->load->view('admin/select_prolist', $this->data);
    }


    public function check_admin()
    {
        if(empty($this->type) || empty($this->pluginid)){
            $this->error('参数错误了，请联系66地球。', '');
        }
        if( $this->session->userdata('admin_user_id')<=0){
            exit('Access Denied');
        }
    }
}